#!/bin/bash


Check_Install()
{
    Check_Nginx_Files
    Check_MariaDB_Files
    Check_PHP_Files
    Check_Moodle_Files
    if [[ "$isNginx" = "ok" && "$isDB" = "ok" && "$isPHP" = "ok" && "$isMoodle" = "ok" ]]; then
        Print_Sucess_Info
     else
        Print_Failed_Info
    fi

}

Check_Nginx_Files()
{
    echo "Checking ..."
    isNginx=""
    if [[ -s /usr/local/nginx/conf/nginx.conf && -s /usr/local/nginx/sbin/nginx ]]; then
        echo -e "\033[32m Nginx: OK \033[0m"
        isNginx="ok"
     else
        echo -e "\033[31m Error: Nginx install failed.\033[0m"
    fi
}

Check_MariaDB_Files()
{
    isDB=""
    if [[ -s /usr/local/mariadb/bin/mysql && -s /usr/local/mariadb/bin/mysqld_safe && -s /etc/my.cnf ]]; then
        echo -e "\033[32m MariaDB: OK \033[0m"
        isDB="ok"
     else
        echo -e "\033[31m Error: MariaDB isn't installed.\033[0m"
    fi
}

Check_PHP_Files()
{
    isPHP=""
    if [[ -s /usr/local/php/sbin/php-fpm && -s /usr/local/php/etc/php.ini && -s /usr/local/php/bin/php ]]; then
        echo -e "\033[32m PHP: OK \033[0m"
        isPHP="ok"
     else
        echo -e "\033[31m Error:  PHP install failed.\033[0m"
    fi
}

Check_Moodle_Files()
{
    isMoodle=""
    if [[ -s ${DB_Data_Dir}/moodle/mdl_config.ibd && -s ${Default_Moodle_Dir}/moodledata/lang/zh_cn/moodle.php ]]; then
        echo -e "\033[32m Moodle: OK \033[0m"
        isMoodle="ok"
     else
        echo -e "\033[31m Error: Moodle install failed.\033[0m"
    fi
}

Print_Sucess_Info()
{
    echo "+------------------------------------------------------------------------"
    echo "|        Moodle service for ${DISTRO} Linux Server, Written by Hongm"
    echo "+------------------------------------------------------------------------"
    echo "|    lnmp status manage: moodle {start|stop|reload|restart|kill|status}"
    echo "+------------------------------------------------------------------------"
    echo "|  phpinfo: http://${IP}/info.php"
    echo "|  Prober:  http://${IP}/p.php"
    echo "+------------------------------------------------------------------------"
    echo "|  MySQL/MariaDB root password: ${DB_Root_Password}"
    echo "+------------------------------------------------------------------------"
    echo "|  Your Moodle WebSite: http://${IP}"
    echo "|     Home Directory: ${Default_Moodle_Dir}/moodle"
    echo "|     Data Directory: ${Default_Moodle_Dir}/moodledata"
    echo "+------------------------------------------------------------------------"
    echo -e "\033[32m Install Moodle completed! enjoy it. \033[0m"
}

Print_Failed_Info()
{
    echo -e "\033[31mSorry,Failed to install Moodle!\033[0m"
    echo -e "\033[31m    You can download /root/moodle-install.log from your server,"
    echo -e "        and mail moodle-install.log to hongm@sina.com for help.\033[0m"
}
